/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmClientesBus.java
 *
 * Created on 02-sep-2011, 22:23:41
 */

package GUI;

import TablasAuxiliares.TACamposPremisas;
import TablasAuxiliares.TATiposPremisas;
import Util.Varios;

/**
 *
 * @author Javi
 */
public class frmPremisasBus extends javax.swing.JDialog {

    //Referencia al formulario padre
    private frmPremisas _frmPadre;

    public frmPremisasBus(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public frmPremisasBus(frmPremisas parent, boolean modal) {
        super(parent, modal);
        this._frmPadre = parent;
        initComponents();
        //Los combos se cargan a partir de las tablas 1 SOLA VEZ, al crear el formulario.
        this.cargarCombos();
        //Actualizo el título de la ventana
        this.setTitle( this._frmPadre.getoObjetoDatos().getsDescripTabla() + " - " + Varios.getLiteralOperacion('S') );
    }

    public void inicializaFormulario ( char cOperacion ){
        //El formulario se vacía cada vez que se entra en él
        this.inicializaControles();
        getRootPane().setDefaultButton(jButtonAceptar);
    }

    /*
     * Inicializa los combos a partir de las tablas auxiliares en forma "codigo - descripcion"
     */
    private void cargarCombos(){
        Varios.cargaComboCamposPremisas (jCBTabla, new TACamposPremisas (), "");
        //Cargo inicialmente en blanco el combo de campos
        Varios.cargaComboCamposPremisas (jCBCampo, null, "");
        Varios.cargaCombo (jCBTipoPremisa, new TATiposPremisas());
    }

    private void cargarComboCampos (String sTabla){
        if (sTabla.equals ("") )
            //Inicializo el combo con un elemento vacío
            Varios.cargaComboCamposPremisas (jCBCampo, null, "");
        else
            Varios.cargaComboCamposPremisas (jCBCampo, new TACamposPremisas (), sTabla);
    }

    /*
     * Este método inicializa los controles de la pantalla
     */
    private void inicializaControles (){
        this.jFTFIdPremisa.setValue(0);
        this.jFTFIdRegla.setValue(0);
        Varios.seleccionaItemCombo (jCBTabla, "");
        Varios.seleccionaItemCombo (jCBCampo, "");
        Varios.seleccionaItemCombo (jCBTipoPremisa, "");
    }

    /**
     * Este método forma la condición a aplicar a partir de los valores introducidos en la pantalla
     * @return
     */
    private String formaCondicionWhere(){
        String sCondicion = "";
        if ((Integer) (this.jFTFIdPremisa.getValue()) != 0)
            sCondicion += " IdPremisa = " + (Integer) (this.jFTFIdPremisa.getValue()) + " AND ";
        if ((Integer) (this.jFTFIdRegla.getValue()) != 0)
            sCondicion += " IdRegla = " + (Integer) (this.jFTFIdRegla.getValue()) + " AND ";
        if (Varios.getClaveItemCombo (jCBTabla).compareTo("")!= 0)
            sCondicion += " Tabla = '" + Varios.getClaveItemCombo (jCBTabla) + "' AND ";
        if (Varios.getClaveItemCombo (jCBCampo).compareTo("")!= 0)
            sCondicion += " Campo = '" + Varios.getClaveItemCombo (jCBCampo) + "' AND ";
        if (Varios.getClaveItemCombo (jCBTipoPremisa).compareTo("")!= 0)
            sCondicion += " TipoPremisa = '" + Varios.getClaveItemCombo (jCBTipoPremisa) + "' AND ";

        //Quitar el último AND de la condición
        if (sCondicion.compareTo("") != 0)
            sCondicion = sCondicion.substring(0, sCondicion.lastIndexOf("AND") );
        return sCondicion;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonAceptar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jFTFIdPremisa = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        jFTFIdRegla = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        jCBTipoPremisa = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jCBTabla = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jCBCampo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButtonAceptar.setText("Aceptar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabel2.setText("IdPremisa:");

        jFTFIdPremisa.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel3.setText("IdRegla:");

        jFTFIdRegla.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel4.setText("Tipo de Premisa:");

        jLabel5.setText("Tabla:");

        jCBTabla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBTablaActionPerformed(evt);
            }
        });

        jLabel6.setText("Campo:");

        jCBCampo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBCampoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCBTabla, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCBTipoPremisa, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jFTFIdRegla, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jFTFIdPremisa, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCBCampo, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(123, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(190, Short.MAX_VALUE)
                .addComponent(jButtonAceptar)
                .addGap(18, 18, 18)
                .addComponent(jButtonCancelar)
                .addGap(168, 168, 168))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jFTFIdRegla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFTFIdPremisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCBTabla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCBCampo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jCBTipoPremisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAceptar)
                    .addComponent(jButtonCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarActionPerformed
        //Actualizo la vista de los datos en el formulario padre con la condición especificada
        this._frmPadre.actualizarVista(this.formaCondicionWhere());
        //Ocultar la ventana solo si el resultado ha sido OK.
        setVisible(false);
}//GEN-LAST:event_jButtonAceptarActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        setVisible( false );
}//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jCBTablaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBTablaActionPerformed
        this.cargarComboCampos (Varios.getClaveItemCombo (jCBTabla));
}//GEN-LAST:event_jCBTablaActionPerformed

    private void jCBCampoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBCampoActionPerformed
}//GEN-LAST:event_jCBCampoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JComboBox jCBCampo;
    private javax.swing.JComboBox jCBTabla;
    private javax.swing.JComboBox jCBTipoPremisa;
    private javax.swing.JFormattedTextField jFTFIdPremisa;
    private javax.swing.JFormattedTextField jFTFIdRegla;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    // End of variables declaration//GEN-END:variables

}
